<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test title</title>
<style type="text/css">
div{width:800px;}
#head{height:200px; background:#CC6699; z-index:1; position:absolute;} /* 这里也需要修改 */
#body{background:#FFCC99; padding-top:200px;} /* 这里也需要修改 */
#foot{height:500px; background:#CCFFFF;}
</style>
</head>
<body>
<div id="head">
</div>
<div id="body">ABC<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />
a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />
a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />a<br />
a<br />a<br />a<br />a<br />a<br />a<br />a<br />xxx<br />
</div>
<div id="foot">
</div>
</body>
</html>
<!-- 重点就在下面的这段 JS 上 --->
<script type="text/javascript">
var head = document.getElementById("head");
var bodyHeight = document.getElementById("body").offsetHeight;
var foot = document.getElementById("foot");
var footHeight = foot.offsetHeight;
window.onscroll = function() {
    var t = document.documentElement.scrollTop || document.body.scrollTop;
    if (t < 150) return; // t < 150 的时候什么都不做
    if (t > 150 && t < (bodyHeight - window.innerHeight + 160)) {
        head.style.top = t - 150;
    }
}
</script>